type PromiseFn = () => Promise<any>

const promiseMap = new Map<any, Promise<any>>()

/**
 *  缓存promise
 * @param key 缓存的promise的key
 * @param promiseFn 该函数返回一个promise函数
 * @returns 返回一个promise函数
 */
export function cachePromise(key: string, promiseFn: PromiseFn): Promise<any> {
  if (promiseMap.has(key)) {
    return promiseMap.get(key)
  }

  const promise = promiseFn()
  promiseMap.set(key, promise)

  return promise
}
